<?php
class category extends CI_Controller
{
    public function __construct(){
        parent::__construct();
        $this->load->library('pagination');
        $this->load->helper('url');
        $this->load->model('category_model');
    }
    public function index(){
        $data['title'] = "List Category";
        $data['path'] = 'category/list';

        $config['uri_segment'] = 4;
        $config['base_url'] = base_url().'admincp/category/index';
        //css pag
        $config['full_tag_open'] = '<ul class="tsc_pagination tsc_paginationA tsc_paginationA01">';
        $config['full_tag_close'] = '</ul>';
        $config['prev_link'] = '&lt;';
        $config['prev_tag_open'] = '<li>';
        $config['prev_tag_close'] = '</li>';
        $config['next_link'] = '&gt;';
        $config['next_tag_open'] = '<li>';
        $config['next_tag_close'] = '</li>';
        $config['cur_tag_open'] = '<li class="current"><a href="#">';
        $config['cur_tag_close'] = '</a></li>';
        $config['num_tag_open'] = '<li>';
        $config['num_tag_close'] = '</li>';
        $config['first_tag_open'] = '<li>';
        $config['first_tag_close'] = '</li>';
        $config['last_tag_open'] = '<li>';
        $config['last_tag_close'] = '</li>';
        $config['first_link'] = '&lt;&lt;';
        $config['last_link'] = '&gt;&gt;';
        $config['total_rows'] = $this->category_model->totalCategory();
        //end
        $config['per_page'] = 3;  // Số record hiển thị trên 1 trang
        $this->pagination->initialize($config);
        $start = $this->uri->segment(4);

        $data['list_cate'] = $this->category_model->listCategory($config['per_page'],$start);
        $data['list_cate_parent'] = $this->category_model->list_cate();

        $this->load->view('dashboard/index',$data);
    }
    public function moveCategory(){
        $id = $this->uri->segment(4);
        $data['title'] = "Move Category";
        $data['path'] = "category/move";
        $data['errors'] = "";
        $data['info'] = $this->category_model->getdata($id);
        $data['list_parent'] = $this->category_model->cate_move($id);
        if($this->input->post('submit') != NULL){
            $parent = $this->input->post('category');
            $dataarray = array(
                "cate_parent" => $parent
            );
            $this->category_model->update($id,$dataarray);
            redirect(base_url()."admincp/category");
        }else{
            $this->load->view('dashboard/index',$data);
        }
    }
    public function addCategory(){
        $data['title'] = "Insert Category";
        $data['path'] = 'category/add';
        $data['errors'] = "";
        $data['list_parent'] = $this->category_model->list_cate();
        if($this->input->post('submit') != NULL){
            $this->form_validation->set_rules('name','Category Name','required');
            if($this->form_validation->run() == NULL){
                $this->load->view('dashboard/index',$data);
            }else{
                $name = $this->input->post('name');
                $cate_parent = $this->input->post('category');
                $description = $this->input->post('description');

                $dataarray = array(
                    'cate_name' => $name,
                    'cate_parent' => $cate_parent,
                    'cate_description' => $description
                );
                $checkName = $this->category_model->checkName($name);
                if($checkName == FALSE){
                    $data['errors'] = "Tên danh mục đã tồn tại";
                    $this->load->view('dashboard/index',$data);
                }else{
                    $this->category_model->addCategory($dataarray);
                    redirect(base_url()."admincp/category");
                    exit();
                }
            }
        }else{
            $this->load->view('dashboard/index',$data);
        }
    }
    public function updateCategory(){
        $id = $this->uri->segment(4);
        $data['title'] = "Edit Category";
        $data['path'] = "category/edit";
        $data['errors'] = "";

        $data['info'] = $this->category_model->getdata($id);
        $data['list_parent'] = $this->category_model->cate_move($id);

        if($this->input->post('submit') != NULL){
            $this->form_validation->set_rules('name','CategoryName','required');
            if($this->form_validation->run()  == NULL){
                $this->load->view('dashboard/index',$data);
            }else{
                $name = $this->input->post("name");
                $description = $this->input->post("description");
                $parent = $this->input->post('category');

                $dataarray = array(
                    "cate_name" => $name,
                    "cate_parent" => $parent,
                    "cate_description" => $description
                );
                $checkCatename = $this->category_model->checkName_update($id,$name);

                if($checkCatename == FALSE){
                    $data['errors'] = "Category đã tồn tại";
                    $this->load->view('dashboard/index',$data);
                }else{
                    $this->category_model->update($id,$dataarray);
                    redirect(base_url()."admincp/category");
                    exit();
                }
            }
        }else{
            $this->load->view('dashboard/index',$data);
        }
    }
    public function deleteCategory(){
        $id = $this->uri->segment(4);
        $this->category_model->deleteCategory($id);
        redirect(base_url().'admincp/category');
        exit();
    }
}